package cn.marinda.gui;

import cn.marinda.MinecraftServerHelper;
import cn.marinda.entity.ComponentSource;
import cn.marinda.gui.impl.ServerConsoleHelperImpl;
import cn.marinda.listener.*;
import cn.marinda.util.FileUtils;
import cn.marinda.util.HttpUtils;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.TableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class AbstractHelper implements ServerHelper{
    protected ArrayList<Component> componentList = new ArrayList<>();
    public JFrame frame;
    public JPanel view;
    protected Font font = new Font(null,Font.BOLD,20);
    protected JFileChooser fileChooser;
    protected  Map<String,List<ComponentSource>> componentMap = new HashMap<>();
    protected Color colorChooser;
    protected boolean isOpen = false;
    protected boolean isClose = false;
    protected Box boxPanel = Box.createVerticalBox();
    protected JMenuBar menuBar;
    protected JScrollPane scrollPane = new JScrollPane();
    protected JToolBar toolBar = new JToolBar();
    protected JPopupMenu popupMenu;
    protected JInternalFrame internalFrame;
    protected String selectRadioText;
    //    单选框数据
    protected Map<String,List<JRadioButton>> createRadioMap = new HashMap<>();
    //    文本编辑框数据
    protected Map<String,JTextField> fieldTextMap = new HashMap<>();
    //    菜单map
    protected Map<String, List<String>> menuItemsMap = new HashMap<>();

    protected List<String> onlineList = new ArrayList<>();


    /**
     * 抽象助手
     */
    public AbstractHelper(){
        registerFrame();
    }

    /**
     * 抽象助手
     *
     * @param onlineList 在线列表
     */
    public AbstractHelper(List<String> onlineList){
        registerOnlineFream(onlineList);
    }



    /**
     * 加载主窗体
     *
     * @return {@link JFrame}
     */
    protected  abstract JFrame loadFrame();

    /**
     * 加载自定义面板
     *
     * @return {@link JPanel}
     */
    protected  abstract JPanel loadPanel();

    /**
     *
     * 加载自定义菜单项
     *
     * @return {@link JMenuBar}
     */

    protected JMenuBar loadMenuBar() {
//        一级菜单
        JMenu choice = new JMenu("选择");
        String[] choiceArray = new String[]{"服务端创建","在线核心","在线插件"};
        String[] serverArray = new String[]{"控制台","核心修改"};
        String[] settingArray = new String[]{"检查更新"};
        String[] aboutArray = new String[]{"关于作者","反馈内容"};
        JMenu server = new JMenu("服务器");
        JMenu setting = new JMenu("设置");
        JMenu aboutAuthor = new JMenu("帮助");
        Map<JMenu,List<JMenuItem>> menuListMap = new HashMap<>();
        List<JMenuItem> aboutItem = createJMenuItem(aboutArray);
        List<JMenuItem> choiceItem = createJMenuItem(choiceArray);
        List<JMenuItem> serverItem = createJMenuItem(serverArray);
        List<JMenuItem> settingItem = createJMenuItem(settingArray);
        choiceItem.get(choiceItem.size()-1).addActionListener(new OpenOnlinePluginEvent());
        choiceItem.get(choiceItem.size()-2).addActionListener(new OpenOnlineCoreEvent());
        serverItem.get(0).addActionListener(new ChangeServerConsoleActivityEvent());
        serverItem.get(1).addActionListener(new OpenSettingSelectServerCoreFileEvent());
        choiceItem.get(0).addActionListener(new OpenCreateServerEvent());
        settingItem.get(0).addActionListener(new OpenCheckVersionEvent());
        aboutItem.get(0).addActionListener(new OpenAuthorInfoEvent());
        aboutItem.get(1).addActionListener(new OpenGroupInfoEvent());
        menuListMap.put(server,serverItem);
        menuListMap.put(setting,settingItem);
        menuListMap.put(choice,choiceItem);
        menuListMap.put(aboutAuthor,aboutItem);
        JMenuBar menuBar = createMenuBar(menuListMap);
        menuBar.add(aboutAuthor);
        return menuBar;
    }

    /**
     * 加载组件
     *
     * @return {@link List}<{@link Component}>
     */
    protected abstract List<Component> loadComponent();


    /**
     * 注册界面
     */
    protected void registerFrame(){
        loadComponent();
        loadMenuBar();
        loadPanel();
        loadFrame();
        frame.setIconImage(HttpUtils.loadLogo());
//        isOpen = true;
    }

    /**
     * 注册在线fream
     *
     * @param list 列表
     */
    protected void registerOnlineFream(List<String> list){
        this.onlineList = list;
        registerFrame();
    }


    /**
     * 得到单选选择的文本
     *
     * @param radioButtons 单选按钮
     * @return {@link String}
     */
    protected String getRadioSelectText(List<JRadioButton> radioButtons){
        radioButtons.stream().forEach(element->{
            if(element.getModel().isSelected()){
                this.selectRadioText = element.getText();
            }
        });
        return this.selectRadioText;
    }

    /**
     * 查询单选按钮文本内容为参数的控件
     *
     * @param text 文本
     * @return {@link JRadioButton}
     */
    protected JRadioButton selectRadioButtonByText(String text){
        for(Component component : componentList){
            if(component instanceof JRadioButton){
                JRadioButton radioButton = (JRadioButton) component;
                if(radioButton.getText().equalsIgnoreCase(text)){
                    return radioButton;
                }
            }
        }
        return null;
    }
    /**
     * 创建文本组件
     *
     *
     * @param text 文本
     * @return {@link JLabel}
     */
    protected JLabel createTextComponent(String text,int align){
        JLabel label = new JLabel(text,align);
        label.setFont(font);
        label.setForeground(Color.black);
        return label;
    }

    /**
     * 创建文本组件
     *
     *
     * @param text 文本
     * @return {@link JLabel}
     */
    protected JLabel createTextComponent(String text){
        JLabel label = new JLabel(text);
        label.setFont(font);
        label.setForeground(Color.black);
        return label;
    }


    /**
     * 创建菜单栏
     *
     * @param menuListMap 菜单列表图
     * @return {@link JMenuBar}
     */
    public JMenuBar createMenuBar(Map<JMenu,List<JMenuItem>> menuListMap){
        menuBar = new JMenuBar();
        for(JMenu menu : menuListMap.keySet()){
            for(JMenuItem jMenuItem : menuListMap.get(menu)){
                menu.add(jMenuItem);
            }
            menuBar.add(menu);
        }

        return menuBar;
    }

    /**
     * 通过uiid来得到一个组件
     *
     * @param componentUIID 组件uiid
     * @return {@link Component}
     */
    public  Component getHelperComponent(String componentUIID){
        Component componentData = null;
        for(Component component:  componentList){
            JComponent jComponent = (JComponent) component;
            if(jComponent.getUIClassID().equalsIgnoreCase(componentUIID)){
                componentData = component;
            }
        }
        return componentData;
    }

    /**
     * 查询text为text内容的Button控件
     *
     * @param text 文本
     * @return {@link Component}
     */
    public JButton getHelperComponentButtonByText(String text){
        for(Component component:  componentList){
            if(component instanceof JButton){
                JButton btn = (JButton) component;
                if(btn.getText().equalsIgnoreCase(text)){
                    return btn;
                }
            }
        }
        return null;
    }

    /**
     * 被文本辅助组件标签
     *
     * @param text 文本
     * @return {@link JLabel}
     */
    public JLabel getHelperComponentLabelByText(String text){
        for(Component component:  componentList){
            if(component instanceof JLabel){
                JLabel label = (JLabel) component;
                if(label.getText().equalsIgnoreCase(text)){
                    return label;
                }
            }
        }
        return null;
    }

    /**
     * 通过uuid 获得列表组件
     *
     * @param componentUIID 组件uiid
     * @return {@link List}<{@link Component}>
     */
    public  List<Component> getHelperComponentList(String componentUIID){
        ArrayList<Component> componentsList = new ArrayList<>();
        for(Component component:  componentList){
            JComponent jComponent = (JComponent) component;
            if(jComponent.getUIClassID().equalsIgnoreCase(componentUIID)){
                componentsList.add(component);
            }
        }
        return componentsList;
    }


    /**
     *
     * 创建列表菜单项
     *
     * @param args arg游戏
     * @return {@link List}<{@link JMenuItem}>
     */
    public List<JMenuItem> createJMenuItem(String[] args){
        List<JMenuItem> menuItemList = new ArrayList<>();
        for(String key : args){
            menuItemList.add(new JMenuItem(key));
        }
        return menuItemList;
    }



    /**
     * 创建工具栏
     *
     * @param components 组件
     * @return {@link JToolBar}
     */
    public JToolBar createToolBar(Component ...components){
        JToolBar toolBar = new JToolBar();
        for(int i = 0;i<components.length;i++){
            toolBar.add(components[i]);
        }
        return toolBar;
    }




    /**
     * 创建弹出酒吧
     *
     * @param menuItem 菜单项
     * @return {@link JPopupMenu}
     */
    public JPopupMenu createPopupBar(JMenuItem ...menuItem){
        JPopupMenu menuBar = new JPopupMenu();
        for(int i = 0;i<menuItem.length;i++){
            menuBar.add(menuItem[i]);
        }
        return menuBar;
    }


    /**
     * 创建内部帧
     *
     * @return {@link JInternalFrame}
     */
    public JInternalFrame createInternalFrame(){
        JInternalFrame frame = new JInternalFrame(
                "内部窗口",  // title
                true,       // resizable
                true,       // closable
                true,       // maximizable
                true       // iconifiable);
        );
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        return frame;
    }


    /**
     * 创建文本组件
     *
     *
     * @param icon 图标
     * @return {@link JLabel}
     */
    protected JLabel createTextComponent(ImageIcon icon){
        JLabel label = new JLabel(icon);
        label.setFont(font);
        label.setForeground(Color.black);
        return label;
    }

    /**
     * 创建按钮组件
     *
     * @param text 文本
     * @return {@link Component}
     */
    protected JButton createButtonComponent(String text){
        JButton button = new JButton(text);
        button.setFont(font);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setForeground(Color.WHITE);
        return button;
    }


    /**
     * 创建按钮组件
     *
     * @param text 文本
     * @return {@link JButton}
     */
    protected JButton createButtonComponent(String text,Color color){
        JButton button = new JButton(text);
        button.setFont(font);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setBackground(Color.RED);
        button.setForeground(color);
        return button;
    }



    /**
     * 创建按钮组件
     *
     * @param icon 图标
     * @return {@link Component}
     */
    protected JButton createButtonComponent(ImageIcon icon){
        JButton button = new JButton(icon);
        button.setBorderPainted(false);
        button.setDefaultCapable(false);
        return button;
    }

    /**
     * 创建密码字段组件
     *
     * @param length   长度
     * @param echoChar 回声字符
     * @return {@link JPasswordField}
     */
    protected JPasswordField createPasswordFieldComponent(int length,char echoChar){
        JPasswordField passwordField = new JPasswordField(length);
        passwordField.setEchoChar(echoChar);
        passwordField.setSelectedTextColor(Color.pink);
        passwordField.setForeground(Color.black);
        return passwordField;
    }

    /**
     * 创建文本框组件
     *
     * @param length 长度
     * @return {@link JTextField}
     */
    protected JTextField createTextFieldComponent(int length){
        JTextField textField = new JTextField(length);
        textField.setSelectedTextColor(Color.pink);
        textField.setForeground(Color.black);
        return textField;
    }


    /**
     * 创建组合框组件
     *
     * @param objData obj数据
     * @return {@link JComboBox}
     */
    protected JComboBox createComboBoxComponent(Object[] objData){
        ComboBoxModel comboBoxModel = new DefaultComboBoxModel(objData);
        JComboBox comboBox = new JComboBox(comboBoxModel);
        comboBox.setFont(new Font(null,Font.PLAIN,20));
        comboBox.setForeground(Color.RED);
        comboBox.setSelectedIndex(0);
        return comboBox;
    }

    protected JList createListComponent(Object[] objData){
        JList list = new JList(objData);
        list.setFont(new Font(null,Font.PLAIN,20));
        list.setSelectionForeground(Color.RED);
        list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        list.setForeground(Color.black);
        return list;
    }

    /**
     * 创建文件选择器
     *
     * @return {@link JFileChooser}
     */
    protected JFileChooser createFileChooser(){
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setSelectedFile(new File("."));
        fileChooser.setCurrentDirectory(new File("."));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("image(*.jpg,*.gif,*.png)","jpg","png","gif"));
        fileChooser.addChoosableFileFilter(new FileNameExtensionFilter("jar(*.jar)","jar"));
        fileChooser.setFileFilter(new FileNameExtensionFilter("txt(*.txt,*.yml)","txt","yml"));
        this.fileChooser = fileChooser;
        return fileChooser;
    }

    /**
     * 创建颜色选择器即打开
     *
     * @return {@link Color}
     */
    protected Color createColorChooser(){
        Color colorChooser = JColorChooser.showDialog(frame,"颜色取色器",null);
        this.colorChooser = colorChooser;
        return colorChooser;
    }

    /**
     * 创建滑块组件
     *
     * @param value 价值
     * @param max   马克斯
     * @param min   最小值
     * @return {@link JSlider}
     */
    protected JSlider createSliderComponent(int value,int max,int min){
        JSlider slider = new JSlider();
        slider.setValue(value);
        slider.setMaximum(max);
        slider.setMinimum(min);
        return slider;
    }

    /**
     * 创建进度条
     *
     * @param value 价值
     * @param max   马克斯
     * @param min   最小值
     * @return {@link JProgressBar}
     */
    protected JProgressBar createProgressBar(int value,int max,int min){
        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(value);
        progressBar.setMaximum(max);
        progressBar.setMinimum(min);
        return progressBar;
    }

    /**
     * 创建文本区域组件
     *
     * @param row    行
     * @param column 列
     * @return {@link JTextArea}
     */
    protected JTextArea createTextAreaComponent(int row,int column){
        JTextArea textArea = new JTextArea(row,column);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setFont(new Font(null,Font.BOLD,20));

        textArea.setSelectedTextColor(Color.DARK_GRAY);
        textArea.setForeground(Color.PINK);
        return textArea;
    }

    /**
     * 创建复选框组件
     *
     * @return {@link JCheckBox}
     */
    protected JCheckBox createCheckBoxComponent(String text){
        JCheckBox checkBox = new JCheckBox();
        checkBox.setText(text);
        checkBox.setFont(font);
        checkBox.setForeground(Color.BLACK);
        return checkBox;
    }

    /**
     * 创建复选框组件
     *
     * @param icon 图标
     * @return {@link JCheckBox}
     */
    protected JCheckBox createCheckBoxComponent(ImageIcon icon){
        JCheckBox checkBox = new JCheckBox(icon);
        return checkBox;
    }

    /**
     * 创建单选按钮组件
     *
     * @param text 文本
     * @return {@link JRadioButton}
     */
    protected JRadioButton createRadioButtonComponent(String text){
        JRadioButton button = new JRadioButton();
        button.setText(text);
        button.setFont(font);
        button.setForeground(Color.BLACK);
        return button;
    }

    /**
     * 创建表组件
     *
     * @param tableModel 表格模型
     * @return {@link JTable}
     */
    protected JTable createTableComponent(TableModel tableModel){
        JTable jTable = new JTable(tableModel);
        jTable.setGridColor(Color.gray);
        jTable.setSelectionBackground(Color.GREEN);
        jTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jTable.setCellSelectionEnabled(true);
        return jTable;
    }

    /**
     * 创建树组件
     *
     * @param rootTree 根树
     * @return {@link JTree}
     */
    protected JTree createTreeComponent(DefaultMutableTreeNode rootTree){
        JTree tree = new JTree(rootTree);
        return tree;
    }

    @Override
    public boolean open() {
        if(!isOpen){
            isOpen = true;
            isClose = false;
            frame.setVisible(true);
            return true;
        }
        isOpen = false;
        isClose = true;
        return true;

    }

    @Override
    public boolean close() {
        if(isOpen){
            isClose = true;
            isOpen = false;
            frame.setVisible(false);
            return true;
        }
        isClose = false;
        return true;
    }

    @Override
    public boolean isOpen() {
        return isOpen;
    }

    @Override
    public boolean isClose() {
        return isClose;
    }

    /**
     * 创建小组
     *
     * @param layoutManager 布局管理器
     * @return {@link JPanel}
     */
    protected JPanel createPanel(LayoutManager layoutManager){
        JPanel panel = new JPanel(layoutManager);
        return panel;
    }



    /**
     * 创建dia日志
     *
     * @return {@link JDialog}
     */
    protected  JDialog createDiaLog(){
        JDialog jDialog = new JDialog();
        return jDialog;
    }




    @Override
    public ArrayList<Component> getComponents() {
        return componentList;
    }
}
